package com.item.action;

import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.base.Page;
import com.base.dialect.PaginationSupport;
import com.base.util.JSONUtils;
import com.base.util.StringUtil;
import com.item.dao.model.IndexImg;
import com.item.dao.model.IndexImgExample;
import com.item.exception.JSONRepUtils;
import com.item.service.IndexImgService;

@Controller
public class IndexImgController {
	@Autowired
	private IndexImgService indexImgService;
	
	@RequestMapping("/listIndexImg")
	@ResponseBody
	public String list(Page param, Integer isNotPage, IndexImg indexImg, Integer isM) throws Exception{
		Object objForSer = null;
		IndexImgExample example = new IndexImgExample();
		PaginationSupport.setContext(param.getPageStart(), param.getPageSize(), null);

		example.setOrderByClause("order_value ASC");
		List<IndexImg> list = indexImgService.selectByExample(example);
		if(isNotPage != null && isNotPage == 1) {
			objForSer = list;
		} else {
			objForSer = new Page(PaginationSupport.getContext().getTotalCount(), list);
		}
		return JSONRepUtils.serialize(objForSer, isM);
	}

	
	@RequestMapping("/showlistIndexImg")
	public String list() {
		return "indexImg/indexImgList";
	}
	
	@RequestMapping("/toEditIndexImg")
	public String toEdit()throws Exception{
		return "indexImg/indexImgEdit";
	}
	
	@RequestMapping(value="/saveIndexImg",method=RequestMethod.POST)
	@ResponseBody
	public String save(IndexImg indexImg, Integer isTop, HttpSession session)throws Exception{		
		try{
			if(StringUtil.isEmpty(indexImg.getId())){
				indexImg.setCreateTime(new Date());
				indexImgService.insertSelective(indexImg);
			} else {
				indexImg.setModifyTime(new Date());
				indexImgService.updateByPrimaryKeySelective(indexImg);
			}
		}catch(Exception e){
			e.printStackTrace();
			return "{\"success\":false}";
		}
		
		return "{\"success\":true}";
	}
	
	@RequestMapping(value="/findByIdIndexImg")
	@ResponseBody
	public String findById(String id,HttpSession session)throws Exception{		
		IndexImg n = indexImgService.selectByPrimaryKey(id);

		return JSONUtils.serialize(n);
	}
	
	@RequestMapping("/delIndexImg")
	@ResponseBody
	public String del(String id,String url,HttpSession session)throws Exception{
		String[] ids = id.split(",");
		int len = ids.length;
		for(int i = 0; i < len; i++) {
			indexImgService.deleteByPrimaryKey(ids[i]);
		}
		return "{\"success\":true}";
	}
}

